/*
 *  Copyright 2016 Google Inc. All Rights Reserved.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package com.google.blockly.ohos.demo.unit;

import com.google.blockly.model.Field;
import com.google.blockly.model.FieldCheckbox;
import org.junit.Assert;
import org.junit.Test;


/**
 * Tests for {@link FieldCheckbox}.
 */
public class FieldCheckboxTest {
    @Test
    public void testFieldCheckbox() {
        FieldCheckbox field = new FieldCheckbox("checkbox", true);
        Assert.assertEquals(Field.TYPE_CHECKBOX, field.getType());
        Assert.assertEquals("checkbox", field.getName());
        Assert.assertEquals(true, field.isChecked());
        field.setChecked(false);
        Assert.assertEquals(false, field.isChecked());

        field = new FieldCheckbox("fname", false);
        Assert.assertEquals(false, field.isChecked());

        field.setChecked(true);

        Assert.assertEquals(true, field.isChecked());
        Assert.assertTrue(field.setFromString("false"));
        Assert.assertFalse(field.isChecked());

        Assert.assertTrue(field.setFromString("true"));
        Assert.assertTrue(field.setFromString("TRUE"));
        Assert.assertTrue(field.setFromString("True"));
        Assert.assertTrue(field.isChecked());

        // xml parsing
        // Boolean.parseBoolean checks the lowercased value against "true" and returns false

        Assert.assertTrue(field.setFromString("This is not a boolean"));
        Assert.assertFalse(field.isChecked());

        field.setChecked(true);
        Assert.assertTrue(field.setFromString("t"));
        Assert.assertFalse(field.isChecked());
    }

    @Test
    public void testClone() {
        FieldCheckbox field = new FieldCheckbox("checkbox", true);
        FieldCheckbox clone = field.clone();

        Assert.assertNotSame(clone, field);
        Assert.assertEquals(field.getName(), field.getName());
        Assert.assertEquals(field.isChecked(), clone.isChecked());

        // Test with false
        field = new FieldCheckbox("checkbox", false);
        clone = field.clone();
        Assert.assertEquals(field.isChecked(), clone.isChecked());

    }

}
